﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace ShutUp
{
    public partial class UpdateForm : Form
    {
        public UpdateForm()
        {
            InitializeComponent();
        }

        private readonly ShutUp _ShutUp = new ShutUp();

        private void UpdateForm_Load(object sender, EventArgs e)
        {
            VersionLabel.Text = "v" + _ShutUp.Version.ToString();
            lb_Version.Text = "Version: " + _ShutUp.Version.ToString();
            BarryD.Image = Image.FromFile(Application.StartupPath + "\\Plugins\\ShutUp\\Forms\\BarryDurex");
            Icon = ChatHelper.convertToIcon(BarryD.Image, System.Drawing.Color.Empty);

            chb_autoUpdate.Checked = ShutUpGlobalSettings.Instance.UpdateCheck;

            if (ShutUpGlobalSettings.Instance.UpdateCheck_last != null)
            {
                var dt = ShutUpGlobalSettings.Instance.UpdateCheck_last;
                lb_lastCheck.Text = string.Format("last check: {0} - ({1})", dt.ToShortDateString(), dt.ToShortTimeString());
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            try
            {
                WebClient wClient = new WebClient();
                string strSource = wClient.DownloadString("http://shutup.googlecode.com/svn/trunk/version/version.txt");
                Version _v = new Version(strSource);

                if (_v != _ShutUp.Version)
                {
                    string _msg = string.Format("a new version is available! \n Version: {0} \nDo you want to visit the forum now? {0}", strSource);

                    if (MessageBox.Show(_msg, "new Version", MessageBoxButtons.OKCancel) == DialogResult.OK)                    
                        System.Diagnostics.Process.Start(_ShutUp.Forumlink);                    
                }

                ShutUpGlobalSettings.Instance.UpdateCheck_last = DateTime.Now;
                var dt = ShutUpGlobalSettings.Instance.UpdateCheck_last;
                lb_lastCheck.Text = string.Format("last check: {0} - ({1})", dt.ToShortDateString(), dt.ToShortTimeString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Version Fehler \n\n{0}", ex), "Fehler", MessageBoxButtons.OK);
            }
        }

        private void UpdateForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ShutUpGlobalSettings.Instance.UpdateCheck = chb_autoUpdate.Checked;
            ShutUpGlobalSettings.Instance.Save();
        }


        #region toolStripMenuItem
        private void toolStripMenuItem_Open_Click(object sender, EventArgs e)
        {
            var _configForm = new SettingsForm();
            _configForm.Show();
        }
        #endregion
    }
}
